package com.eliteams.quick4j.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eliteams.quick4j.core.entity.OApiException;
import com.eliteams.quick4j.core.util.JSONUtil;
import com.eliteams.quick4j.web.bean.Visitor;
import com.eliteams.quick4j.web.interceptors.VisitorsClient;
import com.eliteams.quick4j.web.interceptors.helper.AuthHelper;

/**
 * 访客pc
 * @author lin35
 * 
 */
@Controller
@RequestMapping("/dingding/visitorpc")
public class VisitorsPcController {
	
	@Resource
	private VisitorsClient visitorsClient;

	/**
	 * 首页
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("index")
	public String index(ModelMap modelMap, HttpServletRequest request) {
		String corpId = request.getParameter("corpid");
		String appid = request.getParameter("appid");
		String config = AuthHelper.getConfig(request);
		modelMap.addAttribute("config",config);
		modelMap.addAttribute("corpid",corpId);
		modelMap.addAttribute("appid",appid);
		
		try {
			String accessToken = AuthHelper.getAccessToken(corpId);
			modelMap.addAttribute("accessToken",accessToken);
		} catch (OApiException e) {
			e.printStackTrace();
		}
		return "visitors/pc_visitors";
	}
	
	/**
	 * 获取访客list
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("query")
	public void query(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
		JSONObject jsonRet = new JSONObject();
		String corpId = request.getParameter("corpid");
		String appid = request.getParameter("appid");
		String userid = request.getParameter("userid");
		jsonRet.put("corpid",corpId);
		jsonRet.put("appid",appid);
		try {
			String accessToken = AuthHelper.getAccessToken(corpId);
			jsonRet.put("accessToken",accessToken);
			String ret = visitorsClient.queryVisitors(accessToken, userid);
			List<Visitor> visitors = new ArrayList<Visitor>();
			JSONArray jsons = JSONObject.parseArray(ret);
			for (int i = 0; i < jsons.size(); i++) {
				Visitor visitor = new Visitor();
				JSONObject jo = (JSONObject) jsons.get(i);
				visitor.setId((Integer) jo.get("id"));
				visitor.setName((String) jo.get("来客姓名"));
				visitor.setType((String) jo.get("来客类别"));
				visitor.setApprovalStatus((String) jo.get("审核"));
				visitor.setTime((String) jo.get("进校时间"));
				visitor.setInfo((String) jo.get("来访事由"));
				visitors.add(visitor);
			}
			jsonRet.put("pageList",visitors);
			jsonRet.put("success",true);
		} catch (OApiException e) {
			e.printStackTrace();
		}
		JSONUtil.write(response, jsonRet.toString());
	}
	
	/**
	 * 获取访客详情
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("getinfo")
	public String getInfo(ModelMap modelMap, HttpServletRequest request) {
		Long visitorId = Long.parseLong(request.getParameter("visitorId"));
		String accessToken = request.getParameter("accessToken");
		String corpId = request.getParameter("corpid");
		String appid = request.getParameter("appid");
		String config = AuthHelper.getConfig(request);
		modelMap.addAttribute("config",config);
		modelMap.addAttribute("corpid",corpId);
		modelMap.addAttribute("appid",appid);
		modelMap.addAttribute("c",accessToken);
		System.out.println(accessToken+"----------accessToken");
		try {
			String ret = visitorsClient.getVisitor(accessToken,visitorId);
			JSONArray jsons = JSONObject.parseArray(ret);
			Visitor visitor = new Visitor();
			JSONObject json = (JSONObject) jsons.get(0);
			visitor.setId((Integer) json.get("id"));
			visitor.setName((String) json.get("来客姓名"));
			visitor.setType((String) json.get("来客类别"));
			visitor.setApprovalStatus((String) json.get("审核"));
			visitor.setTime((String) json.get("进校时间"));
			visitor.setInfo((String) json.get("来访事由"));
			visitor.setPhone((String) json.get("联系电话"));
			visitor.setOther((String) json.get("访客对象"));
			visitor.setEndTime((String) json.get("离校时间"));
			visitor.setMemo((String) json.get("备注"));
			modelMap.addAttribute("visitor",visitor);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "visitors/pc_visitors_info";
	}
	
	/**
	 * 设置访客状态
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("setret")
	public void setRet(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
		JSONObject jsonRet = new JSONObject();
		Long visitorId = Long.parseLong(request.getParameter("visitorId"));
		String type = request.getParameter("type");
		String accessToken = request.getParameter("c");
		try {
			int i = 0;
			if ("1".equals(type)) {
				i = 1;
			} else if("0".equals(type)){
				i = 2;
			} else {
				
			}
			String ret = visitorsClient.setVisitorRet(accessToken, visitorId, i);
			JSONArray jsons = JSONObject.parseArray(ret);
			System.out.println(jsons);
			jsonRet.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		JSONUtil.write(response, jsonRet.toString());
	}
}
